var local = {
	token: localStorage.getItem("token") || "",
};
Vue.mixin({
	data() {
		return {
			local,
		};
	},
	computed: {
		jwt() {
			let token = this.local.token;
			try {
				let text = atob(token.split(".")[0]);
				console.log(text);
				return JSON.parse(text);
			} catch (error) {}
			return {};
		},
		agent() {
			return this.jwt.account;
		},
	},
	methods: {
		logout() {
			this.local.token = "";
			localStorage.removeItem("token");
		},
		copy(text, e) {
			copy(text, e && e.target);
		},
	},
});

function copy(text, el) {
	if (!text) return;
	let input = document.createElement("textarea");
	input.value = text;
	document.body.appendChild(input);
	input.select();
	document.execCommand("copy");
	document.body.removeChild(input);
	if (el) {
		let prev = el.innerText;
		el.innerText = "已复制";
		setTimeout(() => {
			el.innerText = prev;
		}, 1e3);
	}
	return true;
}

function encodeQuery(data, limit) {
	var ss = [];
	for (var k in data) {
		var v = data[k];
		if (v == null || typeof v === "function") continue;
		if (v === false) v = "";
		if (typeof v === "object") v = JSON.stringify(v);
		else v = v.toString();
		if (v.length > limit) continue;
		ss.push(encodeURIComponent(k) + "=" + encodeURIComponent(v));
	}
	return ss.join("&");
}

function fetchApi(url, opt) {
	opt = opt || {};
	if (local.token) opt.headers = {Authorization: local.token, ...opt.headers};
	return fetch(url, opt)
		.then((x) => x.json())
		.then((x) => {
			if (x.code == 403) {
				local.token = "";
				localStorage.removeItem("token");
				throw x;
			}
			if (x.code) {
				if (!(opt && opt.ignore)) alert(x.msg);
				throw x;
			}
			return x.data;
		});
}

function postApi(url, body) {
	return fetchApi(url, {
		method: "POST",
		body: JSON.stringify(body),
		headers: {
			"Content-Type": "application/json",
		},
	});
}

function sleep(ms) {
	return new Promise((resolve) => setTimeout(resolve, ms));
}

function getDayHour(ms, fixed = 0) {
	let day = ms / 86400e3;
	let hour = (ms - ~~day * 86400e3) / 3600e3;
	let minute = (ms - ~~day * 86400e3 - ~~hour * 3600e3) / 60e3;
	let second = (ms - ~~day * 86400e3 - ~~hour * 3600e3 - ~~minute * 60e3) / 1e3;
	let list = [];
	if (day >= 1) list.push((list.length ? +day.toFixed(fixed) : Math.floor(day)) + "天");
	if (hour >= 1) list.push((list.length ? +hour.toFixed(fixed) : Math.floor(hour)) + "小时");
	if (minute >= 1) list.push((list.length ? +minute.toFixed(fixed) : Math.floor(minute)) + "分");
	if (second >= 1) list.push((list.length ? +second.toFixed(fixed) : Math.floor(second)) + "秒");
	return list.slice(0, 2).join("");
}

function formatError(obj) {
	if (obj && typeof obj === "object") {
		if (typeof obj.msg === "string") return obj.msg;
		if (typeof obj.message === "string") return obj.message;
		if (typeof obj.error === "string") return obj.error;
		for (let k in obj) {
			let v = obj[k];
			if (typeof v === "string") return v;
		}
		for (let k in obj) {
			let v = obj[k];
			if (v && typeof v === "object") return formatError(v);
		}
	}
	return obj + "";
}

function randomString(len) {
	if (len < 1) return "";
	let s = Math.random()
		.toString(36)
		.slice(2, len + 2);
	return s + randomString(len - s.length);
}